from scipy.sparse.linalg import eigsh
import scipy.sparse as sp
import scipy
import utils
import numpy

        
def pls(W, N, dx, dy, batch_no):
    print 'pls %d' % batch_no;
    
    d = 50

    U = W.transpose().dot(W);
    evals_large, evecs_large = eigsh(U, d, which='LM');
    
   
    Lx= sp.csr_matrix(evecs_large);
    
    V = W.dot(W.transpose());

    evals_large2, evecs_large2 = eigsh(V, d, which='LM');
    Ly = sp.csr_matrix(evecs_large2);
    #norm(Lx, d);
    #norm(Lx, d);
    print type(Lx);

    print 'Saving Model For Batch %d' % batch_no;
    utils.save_sparse_csr('Lx_pls_%d.txt.npz'% (batch_no), sp.csr_matrix(Lx));
    utils.save_sparse_csr('Ly_pls_%d.txt.npz'% (batch_no), sp.csr_matrix(Ly));

if __name__ == '__main__':
    config = utils.get_config();
    dx = config.getint('rmls', 'dx');
    dy = config.getint('rmls', 'dy');
    W = utils.load_sparse_csc('W_F.npz');
    N = 0;
    batch_no = -1;
    pls(W, N, dx, dy, batch_no);

    
